#include "../myudp.h"


#define UDP_SERVER_IP   "192.168.31.13"
#define UDP_SERVER_PORT 50000

int main(int argc, char const *argv[])
{
    int Ret;
    int UdpServerSockFd;

    Ret = UdpSelfBind( &UdpServerSockFd, UDP_SERVER_IP, UDP_SERVER_PORT );
    if( -1 == Ret )
    {
        perror( "fail UdpSelfBind" );
        return -1;
    }

    char strRecvBuff[1024] = {0};
    char strClintIP[100] = {0};
    int ClintPort = 0;

    Ret = UdpRecvfrom( UdpServerSockFd, strRecvBuff, sizeof(strRecvBuff), 
                       strClintIP, &ClintPort );
    if( -1 == Ret )
    {
        perror( "fail UdpRecvfrom" );
        return -1;
    }

    printf( "Received from %s : %d : %s\n", strClintIP, ClintPort, strRecvBuff );

    char strSendBuff[200] = {0};
    sprintf( strSendBuff, "Echo from %s : %d\n", UDP_SERVER_IP, UDP_SERVER_PORT );
    Ret = UdpSendto( UdpServerSockFd, strSendBuff, strlen(strSendBuff), strClintIP, ClintPort ); 
    if( -1 == Ret )
    {
        perror( "fail UdpSendto" );
        return -1;
    }

    Ret = close( UdpServerSockFd );
    if( -1 == Ret )
    {
        perror( "fail close" );
        return -1;
    }

    return 0;
}
